package com.yaozheng.vocationaltraining.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.HomeActivity;
import com.yaozheng.vocationaltraining.bean.CarCategoryBean;
import com.yaozheng.vocationaltraining.bean.CarDetailBean;
import com.yaozheng.vocationaltraining.bean.CarTypeBean;
import com.yaozheng.vocationaltraining.bean.CarTypeDetailBean;
import com.yaozheng.vocationaltraining.bean.DownPaymentBean;
import com.yaozheng.vocationaltraining.bean.RepaymentReriodBean;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by moon on 2017/11/14.
 */

public class CarTypeDetailListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//
    private Activity mContext;

    private List<CarDetailBean> data= new ArrayList<>();

    ArrayList<DownPaymentBean> downPaymentBeanList = new ArrayList<>();
    ArrayList<RepaymentReriodBean> rePaymentPeriodBeanList = new ArrayList<>();

    private String categoryName;


    public CarTypeDetailListAdapter(Activity context,
                                  List<CarTypeBean> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        initCarDetailData(data);
    }

    private void initCarDetailData(List<CarTypeBean> carTypeData) {
        data.clear();
        for(CarTypeBean type : carTypeData){
            CarDetailBean group = new CarDetailBean();
            group.setGroup(true);
            group.setName(type.getName());
            data.add(group);

            for (CarDetailBean detail : type.getCars()){
                data.add(detail);
            }
        }
    }

    @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public boolean isEnabled(int position) {
            if (data.get(position).isGroup()){
                return false;
            }
            return super.isEnabled(position);
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view=convertView;
            if(data.get(position).isGroup()){
                view= LayoutInflater.from(mContext).inflate(R.layout.car_detail_group_list_item, null);

                TextView text=(TextView) view.findViewById(R.id.name_textview);
                text.setText(categoryName+" "+data.get(position).getName());
            }else{
                view=LayoutInflater.from(mContext).inflate(R.layout.car_detail_child_list_item, null);
                TextView price_textview = (TextView) view.findViewById(R.id.price_textview);
                LinearLayout calculate_layout = (LinearLayout) view.findViewById(R.id.calculate_layout);

                price_textview.setText(data.get(position).getPrice()+"");
                calculate_layout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ((HomeActivity)mContext).openPriceFragment(data.get(position)
                        ,downPaymentBeanList,rePaymentPeriodBeanList,categoryName);
                    }
                });

                TextView text=(TextView) view.findViewById(R.id.name_textview);
                text.setText(data.get(position).getName());
            }



            return view;
        }

    public void setData(List<CarTypeBean> carTypeBeanList
            , ArrayList<DownPaymentBean> downPaymentBeanList
            ,ArrayList<RepaymentReriodBean> rePaymentPeriodBeanList
            ,String categoryName) {
        this.downPaymentBeanList = downPaymentBeanList;
        this.rePaymentPeriodBeanList = rePaymentPeriodBeanList;
        this.categoryName = categoryName;
        initCarDetailData(carTypeBeanList);
    }
}
